<template>
	<view class="purchaseAppoint">
		<uni-nav-bar
			color="#ffffff"
			background-color="#00aded"
			:statusBar="true"
			title="收货通知单"
			fixed="true"
			left-icon="left"
			left-text="返回"
			@clickLeft="back"
			@clickRight="addClick"
			right-icon="plus"
			rightPermissionAttr="addClick"
		></uni-nav-bar>
		<view class="purchaseAppoint_top">
			<!-- 搜索 -->
			<!-- 搜索 -->
			<dil-search :columnData="options.columnDataList" @onSearch="onSearch" v-if="options.columnDataList.length > 0"></dil-search>
		</view>
		<view class="purchaseAppoint_main">
			<dil-card class="dilCard" @emitEvent="emitEvent" v-bind.sync="options"></dil-card>
		</view>
		<view class="purchaseAppoint_floot"></view>
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
export default {
	data() {
		return {
			preParams: {},
			options: {
				requestUrl: '/api/v1/ams/getPurchaseRequirementList?apiId=534',
				requestQuery: {},
				cardSlotList: [
					{
						label: '授权承运商',
						icon: 'wlIcon-shouquan',
						emitEvent: 'purchasePlanAdd'
					},
					{
						label: '修改',
						icon: 'wlIcon-shouquan',
						emitEvent: 'purchaseAppointEdit'
					},
					{
						label: '删除',
						icon: 'trash',
						emitEvent: 'deleteRequirement',
						arguments: {
							deleted: 5
						}
					},
					{
						label: '运输详情',
						icon: 'bars',
						emitEvent: 'transOrderBrowse'
					}
				],
				columnDataList: [],
				totalNum: 0
			}
		};
	},
	onLoad(options) {
		this.preParams = options;
		this.options.requestQuery = { ...this.preParams };
	},
	onShow() {
		uni.$emit('onShow', 1);
	},
	mounted() {},
	methods: {
		getOrderSectionDebounce() {
			this.options.requestQuery = { ...this.options.requestQuery };
		},
		async purchaseAppointEdit(item) {
			item.requirementNo = item.serialNumber;
			item.navBarTitle = '修改收货通知单';
			item.navBarFlag = 'update';
			item.sbddList = item.sbdd.split(',');
			this.updatePurchaseOrderItem({ ...item });
			uni.navigateTo({
				url: '/pages/transAppoint/purchaseAppointAdd'
			});
		},
		async addClick() {
			const res = await this.$myRequest({
				url: '/api/v1/uc/getRequirementNo',
				data: {
					businessFlag: 'CGXQ'
				}
			}).catch(() => {
				uni.showToast({
					duration: 2000,
					title: '获取通知单号失败',
					icon: 'success'
				});
			});
			if (res.data.status == 'succeed') {
				this.updatePurchaseOrderItem({ ...res.data.data });
			} else {
				uni.showToast({
					duration: 2000,
					title: '获取通知单号失败',
					icon: 'success'
				});
			}
			uni.navigateTo({
				url: '/pages/transAppoint/purchaseAppointAdd'
			});
		},
		back() {
			uni.switchTab({
				url: '/pages/index/index'
			});
		},
		onSearch(item) {
			this.options.requestQuery = { ...JSON.parse(JSON.stringify(item)), ...this.preParams };
		},
		emitEvent(eventName, cardItem) {
			this[eventName](cardItem);
		},
		transOrderBrowse(item) {
			uni.navigateTo({
				url:
					'/pages/transOrder/purchaseTransOrder?params=' +
					JSON.stringify({
						requirementId: item.transRequirementId
					})
			});
		},
		async purchasePlanAdd(cardItem) {
			cardItem.planNumber = cardItem.numberRequire;
			this.updatePurchaseAppointItem({ ...cardItem });
			uni.navigateTo({
				url: '/pages/transPlan/purchasePlanAdd'
			});
		},
		deleteRequirement(cardItem) {
			let that = this;
			uni.showModal({
				cancelText: '取消',
				confirmText: '确认',
				title: '提示',
				content: '确定是否删除',
				success: (res) => {
					if (res.confirm) {
						that.purchaseRequirementChange(cardItem);
					} else if (res.cancel) {
						uni.showToast({
							title: '用户取消操作！',
							icon: 'none',
							duration: 3000
						});
					}
				}
			});
		},
		async purchaseRequirementChange(data) {
			const res = await this.$myRequest({
				url: '/api/v1/ams/purchaseRequirementChange',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'none'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					duration: 2000,
					title: '删除成功',
					icon: 'success'
				});
				this.getOrderSectionDebounce();
			} else {
				uni.showToast({
					duration: 2000,
					title: res.data.message,
					icon: 'none'
				});
			}
		},
		//vuex
		...mapMutations('purchase', ['updatePurchaseAppointItem']),
		//以下是vuex;
		...mapMutations('purchase', ['updatePurchaseOrderItem'])
	}
};
</script>

<style lang="scss" scoped>
.purchaseAppoint {
	width: 100vw;
	height: 100%;
	background-color: #f5f5f5;
	&_top {
		background-color: #fff;
	}
	&_main {
		padding-bottom: 10rpx;
	}
	&_floot {
		background-color: #fff;
	}
}
</style>
